package cn.kgcnews.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateCodeServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	// 生成随机验证码
	public String GenerateCode(int length) {
		int rand;
		char code;
		String randomCode = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			rand = random.nextInt();
			if (rand % 3 == 0) {
				code = (char) ('A' + (char) rand % 26);
			} else {
				code = (char) ('0' + (char) rand % 10);
			}
			randomCode += String.valueOf(code);
		}
		return randomCode;
	}

	// 写入随机生成的验证码，并添加噪点
	public BufferedImage Paint(String code, int width, int height) {

		int mapWidth = width == 0 ? (int) (code.length() * 16) : width;
		int mapHeight = height == 0 ? (int) 35 : height;

		BufferedImage map = new BufferedImage(mapWidth, mapHeight,
				BufferedImage.TYPE_INT_RGB);
		Graphics graph = map.getGraphics();

		graph.setColor(Color.WHITE);
		graph.fillRect(0, 0, mapWidth, mapHeight);
		// 设置字体样式
		graph.setFont(new Font(Font.SANS_SERIF, Font.PLAIN,
				(int) (height * 0.8)));

		// 添加噪点
		Random random = new Random();
		for (int i = 0; i < 50; i++) {
			int x = random.nextInt(map.getWidth());
			int y = random.nextInt(map.getHeight());
			int rgb = GetRandomColor().getRGB();
			map.setRGB(x, y, rgb);
		}

		// 定义每个验证码字符颜色
		Color[] c = { Color.BLACK, Color.RED, Color.BLUE, Color.GREEN,
				Color.ORANGE, Color.BLACK, Color.DARK_GRAY, Color.LIGHT_GRAY };

		int fx = 0;
		for (int i = 0; i < code.length(); i++) {
			int x = random.nextInt(7);
			graph.setColor(c[x]);
			graph.drawString(code.charAt(i) + "", fx, (int) (height * 0.8));
			fx += (width / code.length()) * (Math.random() * 0.3 + 0.8);
		}
		graph.dispose();
		return map;

	}

	// 生成随机颜色
	public Color GetRandomColor() {
		Random random = new Random();
		int r = random.nextInt(256);
		int g = random.nextInt(256);
		int b = random.nextInt(256);
		return new Color(r, g, b);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		 response.setContentType("image/jpeg");
	     response.setDateHeader("expries", -1);
	     response.setHeader("Cache-Control", "no-cache");
	     response.setHeader("Pragma", "no-cache");
	     String code = GenerateCode(4);
	     request.getSession().setAttribute("imgcode", code);
	     ImageIO.write(Paint(GenerateCode(4), 120, 39), "jpg", response.getOutputStream());

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		this.doGet(request, response);
		out.flush();
		out.close();
	}

	public void init() throws ServletException {
		
	}

}
